<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	private $_id;
	
	public function authenticate()
	{
		$user=Users::model()->findByAttributes(array('email'=> $this->username));
		if($user===null)
		{
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		}
		elseif($user->password!==$user->encrypt($this->password))
		{
				$this->errorCode=self::ERROR_PASSWORD_INVALID;
		}
		else
		{
			$this->_id = $user->id;
			$this->setState('type', $user->type);
			$this->setState('fist_name', $user->first_name);
			$this->setState('second_name', $user->second_name);
			$this->errorCode = self::ERROR_NONE;
		}
		return !$this->errorCode;
	}
	
	public function getId()
    {
        return $this->_id;
    }
}